<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Upload 使用记录 | Beiysd</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/assets/logo/pkp.png">
    <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?345ab2bf830dc5c68a96925eb1dd0b45";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    <meta name="description" content="Live and Study.">
    
    <link rel="preload" href="/assets/css/0.styles.bd7bd1ca.css" as="style"><link rel="preload" href="/assets/js/app.17b6b866.js" as="script"><link rel="preload" href="/assets/js/3.16fca902.js" as="script"><link rel="preload" href="/assets/js/2.aa0e2402.js" as="script"><link rel="preload" href="/assets/js/18.6f1b273c.js" as="script"><link rel="prefetch" href="/assets/js/10.71654f5d.js"><link rel="prefetch" href="/assets/js/11.e3df8a38.js"><link rel="prefetch" href="/assets/js/12.f6e1589f.js"><link rel="prefetch" href="/assets/js/13.8ac48caf.js"><link rel="prefetch" href="/assets/js/14.2a93538f.js"><link rel="prefetch" href="/assets/js/15.ed5b285c.js"><link rel="prefetch" href="/assets/js/16.5e1e59d5.js"><link rel="prefetch" href="/assets/js/17.1da7896e.js"><link rel="prefetch" href="/assets/js/19.30d92fa3.js"><link rel="prefetch" href="/assets/js/20.1068c4cc.js"><link rel="prefetch" href="/assets/js/21.f3d90412.js"><link rel="prefetch" href="/assets/js/22.a3c35868.js"><link rel="prefetch" href="/assets/js/23.3dfaf7a4.js"><link rel="prefetch" href="/assets/js/24.b7562cb8.js"><link rel="prefetch" href="/assets/js/25.94bbff07.js"><link rel="prefetch" href="/assets/js/26.9cbdf92e.js"><link rel="prefetch" href="/assets/js/27.e2189a25.js"><link rel="prefetch" href="/assets/js/28.12e893b0.js"><link rel="prefetch" href="/assets/js/29.ab1115c9.js"><link rel="prefetch" href="/assets/js/30.e2112157.js"><link rel="prefetch" href="/assets/js/31.76cdc758.js"><link rel="prefetch" href="/assets/js/32.c94896d8.js"><link rel="prefetch" href="/assets/js/33.0841fa36.js"><link rel="prefetch" href="/assets/js/34.831fd380.js"><link rel="prefetch" href="/assets/js/35.b0449875.js"><link rel="prefetch" href="/assets/js/36.30a8c074.js"><link rel="prefetch" href="/assets/js/37.1aec4dbc.js"><link rel="prefetch" href="/assets/js/38.ccd6cdf9.js"><link rel="prefetch" href="/assets/js/39.c10337d7.js"><link rel="prefetch" href="/assets/js/4.98a034f9.js"><link rel="prefetch" href="/assets/js/40.addec5bb.js"><link rel="prefetch" href="/assets/js/41.e3e62de0.js"><link rel="prefetch" href="/assets/js/42.ba7501f2.js"><link rel="prefetch" href="/assets/js/43.d8fa5ea0.js"><link rel="prefetch" href="/assets/js/44.d6dbcdb3.js"><link rel="prefetch" href="/assets/js/45.fe55507d.js"><link rel="prefetch" href="/assets/js/46.6af0f2df.js"><link rel="prefetch" href="/assets/js/47.0560a688.js"><link rel="prefetch" href="/assets/js/48.ab74b154.js"><link rel="prefetch" href="/assets/js/49.7ed2bcd8.js"><link rel="prefetch" href="/assets/js/5.2ca1c567.js"><link rel="prefetch" href="/assets/js/50.0f22d3af.js"><link rel="prefetch" href="/assets/js/51.ad770490.js"><link rel="prefetch" href="/assets/js/52.bc1bd98c.js"><link rel="prefetch" href="/assets/js/53.9ff88855.js"><link rel="prefetch" href="/assets/js/54.f2b42c40.js"><link rel="prefetch" href="/assets/js/55.3d551cd8.js"><link rel="prefetch" href="/assets/js/56.bbf7abd0.js"><link rel="prefetch" href="/assets/js/57.4b2fbc05.js"><link rel="prefetch" href="/assets/js/58.4ff6654e.js"><link rel="prefetch" href="/assets/js/59.92969875.js"><link rel="prefetch" href="/assets/js/6.f1011be9.js"><link rel="prefetch" href="/assets/js/60.4fb5be24.js"><link rel="prefetch" href="/assets/js/61.661f240d.js"><link rel="prefetch" href="/assets/js/62.d08a0fee.js"><link rel="prefetch" href="/assets/js/63.607fa9fb.js"><link rel="prefetch" href="/assets/js/64.bfa429e4.js"><link rel="prefetch" href="/assets/js/65.e184bc74.js"><link rel="prefetch" href="/assets/js/7.28ea5937.js"><link rel="prefetch" href="/assets/js/8.d804616d.js"><link rel="prefetch" href="/assets/js/9.7977e413.js">
    <link rel="stylesheet" href="/assets/css/0.styles.bd7bd1ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/assets/logo/pkq.png" alt="Beiysd" class="logo"> <span class="site-name can-hide">Beiysd</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://github.com/Beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://blog.csdn.net/weixin_40532650" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://github.com/Beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://blog.csdn.net/weixin_40532650" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>前端</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>CSS</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading open"><span>Antd3.x</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Checkbox.html" class="sidebar-link">Checkbox</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/DatePicker.html" class="sidebar-link">DatePicker</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Input.html" class="sidebar-link">Input</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Modal.html" class="sidebar-link">Modal</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Popover.html" class="sidebar-link">Popover</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Progress.html" class="sidebar-link">Progress</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Select.html" class="sidebar-link">Select</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Table.html" class="sidebar-link">Table</a></li><li><a href="/notes/10-前端/10-CSS/10-Antd3.x/Upload.html" class="active sidebar-link">Upload</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/10-前端/10-CSS/10-Antd3.x/Upload.html#_1-场景" class="sidebar-link">1.场景</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/10-CSS/10-Antd3.x/Upload.html#_2-效果" class="sidebar-link">2.效果</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/10-CSS/10-Antd3.x/Upload.html#_3-业务逻辑" class="sidebar-link">3.业务逻辑</a></li><li class="sidebar-sub-header"><a href="/notes/10-前端/10-CSS/10-Antd3.x/Upload.html#_4-功能实现" class="sidebar-link">4.功能实现</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading"><span>Antd4.x</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading"><span>Css3</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>常用方法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>React</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>JavaScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>Vuepress</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>Taro小程序</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>后端</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>随笔</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>杂谈</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开发相关</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"><h3 class="page_title">Upload 使用记录</h3> <ul class="uls"><li title="标签" class="lis"><img src="/assets/img/tag.f21efeeb.svg" class="icons">
    </li> <li title="更新时间" class="lis"><img src="/assets/img/clock.ffb2d0be.svg" class="icons">2021-04-16
    </li> <li title="浏览人数" class="lis"><img src="/assets/img/eye.e9087eed.svg" class="icons">0
    </li></ul>  <div class="theme-default-content content__default"><h3 id="upload-使用记录"><a href="#upload-使用记录" class="header-anchor">#</a> Upload 使用记录</h3> <h2 id="_1-场景"><a href="#_1-场景" class="header-anchor">#</a> 1.场景</h2> <p>注册上传图片，所有信息填写完毕后统一返回给后端，提交注册</p> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>sessonStorage 关闭会话框即会清空
注意上传文件格式一般为二进制数据流，即 FormData 形式
const form = new FormData()
form.append('file', file)</p> <p>另外 Antd 的 Upload 会把上传文件封装一次，originFileObj 才是源文件
beforeData 方法中传入的 file，是源数据
onChange 方法中传入的 fileList 则是 Upload 自己需要的封装后的数据</p></div> <h2 id="_2-效果"><a href="#_2-效果" class="header-anchor">#</a> 2.效果</h2> <ul><li><p>2-1 保留 Upload 组件自带的预览和删除功能</p> <p><img src="/assets/md-imgs/upload_one.png" alt="保留组件自带的预览和删除功能"></p></li> <li><p>2-2 做了特殊判断，只上传一张，和上传多张图片的处理，本质是对上传按钮的显隐控制</p> <p><img src="/assets/md-imgs/upload_many.png" alt="在这里插入图片描述"></p></li></ul> <h2 id="_3-业务逻辑"><a href="#_3-业务逻辑" class="header-anchor">#</a> 3.业务逻辑</h2> <ul><li>3-1. 上传一张图片，调一次图片接口，返回线上路径，本地存储线上路径（<strong><em>格式为下方代码所示，其中 uid 是 Upload 组件上传时的 uid，path 为后端返回图片路径</em></strong>）</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>多图：<span class="token punctuation">[</span><span class="token punctuation">{</span>id<span class="token operator">:</span>uid<span class="token punctuation">,</span> path<span class="token operator">:</span><span class="token string">'xxx'</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
单个：<span class="token string">'xxx'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>3-2. 预览和删除功能，均使用 Upload 自带方法</li></ul> <h2 id="_4-功能实现"><a href="#_4-功能实现" class="header-anchor">#</a> 4.功能实现</h2> <ul><li>4.1 组件使用</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>Upload
     name<span class="token operator">=</span><span class="token string">'file'</span>
     accept<span class="token operator">=</span><span class="token string">'image/*'</span> <span class="token comment">/*限制上传图片类型文件*/</span>
     listType<span class="token operator">=</span><span class="token string">'picture-card'</span>  <span class="token comment">/*上传列表内建样式*/</span>
     className<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>avatar_uploader<span class="token punctuation">}</span>
     fileList<span class="token operator">=</span><span class="token punctuation">{</span>fileList<span class="token punctuation">}</span> <span class="token comment">/*已经上传的文件列表（受控）*/</span>
     beforeUpload<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token parameter">file</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
     <span class="token comment">/*上传文件之前的钩子，参数为上传的文件，若返回 false 则停止上传*/</span>
       <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">handleUpload</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token comment">/*自定义上传*/</span>
       <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token comment">/*阻止组件自带上传事件*/</span>
     <span class="token punctuation">}</span><span class="token punctuation">}</span>
     onPreview<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>onPreview<span class="token punctuation">}</span><span class="token comment">//预览事件</span>
     onChange<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>handleChange<span class="token punctuation">}</span><span class="token comment">//上传文件更改事件</span>
     onRemove<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>onRemove<span class="token punctuation">}</span><span class="token comment">//删除文件事件</span>
   <span class="token operator">&gt;</span>
   <span class="token comment">//根据传入类型判断上传按钮的显隐</span>
     <span class="token punctuation">{</span>type <span class="token operator">===</span> <span class="token constant">OTHERS</span> <span class="token operator">?</span> uploadButton <span class="token operator">:</span> imageUrl <span class="token operator">?</span> <span class="token keyword">null</span> <span class="token operator">:</span> uploadButton<span class="token punctuation">}</span>
 <span class="token operator">&lt;</span><span class="token operator">/</span>Upload<span class="token operator">&gt;</span>

<span class="token comment">//预览使用的Modal组件</span>
<span class="token operator">&lt;</span>Modal visible<span class="token operator">=</span><span class="token punctuation">{</span>visible<span class="token punctuation">}</span> footer<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">null</span><span class="token punctuation">}</span> onCancel<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>onPreview <span class="token punctuation">}</span><span class="token operator">&gt;</span>
   <span class="token operator">&lt;</span>img alt<span class="token operator">=</span><span class="token string">'加载'</span> style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> width<span class="token operator">:</span> <span class="token string">'100%'</span><span class="token punctuation">,</span> height<span class="token operator">:</span> <span class="token string">'100%'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> src<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'xxx'</span><span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>Modal<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><ul><li>4.2 方法实现</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//上传文件校验</span>
<span class="token keyword">function</span> <span class="token function">beforeUpload</span><span class="token punctuation">(</span><span class="token parameter">file</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
 <span class="token comment">//类型判断</span>
 <span class="token keyword">const</span> isJpgOrPng <span class="token operator">=</span> file<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">'image/jpeg'</span> <span class="token operator">||</span> file<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">'image/png'</span>
 <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>isJpgOrPng<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'请上传图片类型文件！'</span><span class="token punctuation">)</span>
   <span class="token keyword">return</span> <span class="token boolean">false</span>
 <span class="token punctuation">}</span>
 <span class="token comment">//容量判断</span>
 <span class="token keyword">const</span> isLt2M <span class="token operator">=</span> file<span class="token punctuation">.</span>size <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">&lt;</span> <span class="token number">10</span>
 <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>isLt2M<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">'图片大小不得超过10M！'</span><span class="token punctuation">)</span>
   <span class="token keyword">return</span> <span class="token boolean">false</span>
 <span class="token punctuation">}</span>
 <span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>

<span class="token comment">//因为是图片路径存在本地</span>
<span class="token function">componentDidMount</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token comment">//初始化清空</span>
   <span class="token keyword">const</span> <span class="token punctuation">{</span> type <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props
   <span class="token keyword">if</span> <span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token constant">OTHERS</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
     <span class="token function">setSessStorage</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
     <span class="token function">setSessStorage</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>


 <span class="token comment">/**
  * @name onPreview
  * @description 预览-取消预览
  */</span>
 <span class="token function-variable function">onPreview</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
   <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> visible<span class="token operator">:</span> <span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>visible<span class="token punctuation">}</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>


<span class="token comment">/**
  * @name handleUpload
  * @param {Object} file 上传的文件信息
  * @desc 自定义上传
  */</span>
 <span class="token function-variable function">handleUpload</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token parameter">file</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
 <span class="token comment">//文件校验</span>
   <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>beforeUpload<span class="token punctuation">)</span> <span class="token punctuation">{</span>
     <span class="token keyword">return</span>
   <span class="token punctuation">}</span>
   <span class="token comment">//请求接口-返回图片路径fileUrl</span>
   <span class="token keyword">const</span> data <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">apiImageUp</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span>
   <span class="token keyword">if</span> <span class="token punctuation">(</span>data <span class="token operator">&amp;&amp;</span> data<span class="token punctuation">.</span>fileUrl<span class="token punctuation">)</span> <span class="token punctuation">{</span>
     <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> imageUrl<span class="token operator">:</span> data<span class="token punctuation">.</span>fileUrl <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
     <span class="token comment">//多图上传</span>
       <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token constant">OTHERS</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
         <span class="token keyword">const</span> imgUrlData <span class="token operator">=</span> <span class="token function">getSessStorage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
         <span class="token comment">//标记uid为了删除对应路径</span>
         <span class="token keyword">const</span> params <span class="token operator">=</span> imgUrlData <span class="token operator">?</span> imgUrlData<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">{</span> uid<span class="token operator">:</span> file<span class="token punctuation">.</span>uid<span class="token punctuation">,</span> url<span class="token operator">:</span> data<span class="token punctuation">.</span>fileUrl <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
         <span class="token function">setSessStorage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type<span class="token punctuation">,</span> params<span class="token punctuation">)</span>
       <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
         <span class="token function">setSessStorage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type<span class="token punctuation">,</span> data<span class="token punctuation">.</span>fileUrl<span class="token punctuation">)</span>
       <span class="token punctuation">}</span>
     <span class="token punctuation">}</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>

<span class="token comment">/**
* @name onRemove
*  @info {Object} info  Upload组件自己封装的上传信息，实际的上传文件是originFileObj
* @desc 删除图片
*/</span>
 <span class="token function-variable function">onRemove</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">info</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
   <span class="token keyword">const</span> file <span class="token operator">=</span> info<span class="token punctuation">.</span>originFileObj
   <span class="token keyword">const</span> list <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>fileList<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">v</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span>uid <span class="token operator">!==</span> file<span class="token punctuation">.</span>uid<span class="token punctuation">)</span>
   <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> fileList<span class="token operator">:</span> list<span class="token punctuation">,</span> imageUrl<span class="token operator">:</span> <span class="token string">''</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
   <span class="token comment">//多图，根据对应uid删除</span>
   <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token constant">OTHERS</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
     <span class="token keyword">const</span> imgUrlData <span class="token operator">=</span> <span class="token function">getSessStorage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
     <span class="token keyword">const</span> params <span class="token operator">=</span> imgUrlData<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">v</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span>uid <span class="token operator">!==</span> info<span class="token punctuation">.</span>uid<span class="token punctuation">)</span>
     <span class="token function">setSessStorage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type<span class="token punctuation">,</span> params<span class="token punctuation">)</span>
   <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
   <span class="token comment">//单图，直接清空</span>
     <span class="token function">setSessStorage</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">.</span>type<span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>


 <span class="token comment">/**
  * @name handleChange
  * @param {Array}} fileList 上传文件
  * @description 上传文件变化-因为使用了Upload自带图片展示，所以保留此方法
  */</span>
 <span class="token function-variable function">handleChange</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> fileList <span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
   <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> fileList <span class="token punctuation">}</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>


 <span class="token comment">/**
  * @name imageSave
  * @description 单一图片，本地保存-提交时统一上传
  */</span>
 <span class="token function-variable function">imageSave</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
   <span class="token keyword">const</span> <span class="token punctuation">{</span> type <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props
   <span class="token keyword">const</span> <span class="token punctuation">{</span> imageUrlList<span class="token punctuation">,</span> imageUrl <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state
   <span class="token keyword">const</span> params <span class="token operator">=</span> <span class="token punctuation">{</span>
     url<span class="token operator">:</span> type <span class="token operator">===</span> <span class="token constant">OTHERS</span> <span class="token operator">?</span> imageUrlList <span class="token operator">:</span> imageUrl
   <span class="token punctuation">}</span>
   <span class="token function">setSessStorage</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> params<span class="token punctuation">)</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br></div></div></div> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notes/10-前端/10-CSS/10-Antd3.x/Table.html" class="prev">
        Table
      </a></span> <span class="next"><a href="/notes/10-前端/10-CSS/20-Antd4.x/DatePicker.html">
        DatePicker
      </a>
      →
    </span></p></div>  <button>click</button> <div class="back_top_content" data-v-26f75736><!----></div></main></div><div class="global-ui"><MySWUpdatePopup></MySWUpdatePopup></div></div>
    <script src="/assets/js/app.17b6b866.js" defer></script><script src="/assets/js/3.16fca902.js" defer></script><script src="/assets/js/2.aa0e2402.js" defer></script><script src="/assets/js/18.6f1b273c.js" defer></script>
  </body>
</html>
